package cn.tedu.collection;
/**
 * @Author 金帮嘉诚 @Date 2021-06-21 星期一 10:39
 */
/*本类用于测试List接口*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**知识点
 * 单个list的方法操作
 * add()
 * get()
 * indexOf()
 * lastIndexOf()
 * remove()
 * set()
 * subList()
 * */
public class TestList {
    public static void main(String[] args) {
        //
        List<String> list=new ArrayList<String>();//import
        //
        list.add("教父1");
        list.add("教父2");
        list.add("教父3");
        list.add("无间道1");
        list.add("无间道2");
        list.add("无间道3");
        list.add("美国往事");
//        list.add("大力娃");
//        list.add("千顺娃");
//        list.add("头铁娃");
//        list.add("喷火娃");
//        list.add("喷水娃");
//        list.add("隐身娃");
//        list.add("小紫娃");

//        list.clear();
//        System.out.println(list);
        System.out.println("list = " + list);

        System.out.println("list.contains(\"教父1\") = " + list.contains("教父1"));
        System.out.println("list.equals(\"教父1\") = " + list.equals("教父1"));
        System.out.println("list.hashCode() = " + list.hashCode());
        System.out.println("list.isEmpty() = " + list.isEmpty());
        System.out.println("list.remove(\"美国往事\") = " + list.remove("美国往事"));
        System.out.println("list.size() = " + list.size());
        Object[] listArray = list.toArray();
        System.out.println("Arrays.toString(listArray) = " + Arrays.toString(listArray));

        //List特有方法--可以根据索引来操作的方式
        //add get indexOf lastIndexOf
        list.add("刘德华");
        System.out.println("-------------------------------------------------------------------------" +
                "\nlist = " + list);
        list.add(1,"梁朝伟");//在下标一处（下标1位置是第二个）添加元素，后面的元素依次后移
        System.out.println("list = " + list);
        System.out.println("list.get(3) = " + list.get(3));
        list.add(3,"刘德华");
        System.out.println(list);

        System.out.println("list.indexOf(\"刘德华\") = " + list.indexOf("刘德华"));
        System.out.println("list.lastIndexOf(\"刘德华\") = " + list.lastIndexOf("刘德华"));

        //remove根据下标或者根据元素对象
        //set根据下标替换元素
        //sublist按下标截取，含头不含尾
        System.out.println("list.remove(7) = " + list.remove(7));
        System.out.println("list.set(3,\"牛德华\") = " + list.set(3,"牛德华"));//修改，返回原元素值
        System.out.println(list);

        List<String> list1 = list.subList(3, 7);
        System.out.println("list1 = " + list1);

        //集合间操作
        //c0.addAll(index,c)把参数集合c中所有元素添加到原集合c0中,而且可以指定从下标index处开始插入
        //c0.containsAll(c)判断原集合c0 是否包含 参数集合c 中的所有 元素
        //c0.removeAll(Collection<E> c)删除集合c0中所有的 参数集合c中包含的元素，即使有多个
        System.out.println("--------------------------------------------------------------------------------\n" +
                "集合间操作：");
        List<String> list2=new ArrayList<String>();
        list2.add("1");//
        list2.add("2");
        list2.add("3");

        System.out.println("list1.addAll(list2) = " + list1.addAll(list2));
        System.out.println("list1 = " + list1);
        System.out.println("list1.addAll(0,list2) = " + list1.addAll(0,list2));
        System.out.println("list1 = " + list1);

        System.out.println("list1.contains(list2) = " + list1.contains(list2));//f
        System.out.println("list1.containsAll(list2) = " + list1.containsAll(list2));//t

        System.out.println("list1.removeAll(list2) = " + list1.removeAll(list2));
        System.out.println("list1 = " + list1);



    }
}
